package com.taocore.top.title.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.taobao.api.ApiException;
import com.taobao.api.domain.Item;
import com.taobao.api.response.ItemGetResponse;
import com.taocore.top.title.domain.DiagnoseResult;
import com.taocore.top.title.service.AttachmentsChecker;
import com.taocore.top.title.service.BrandsChecker;
import com.taocore.top.title.service.ComparationChecker;
import com.taocore.top.title.service.LengthChecker;
import com.taocore.top.title.service.PropertyWordsChecker;
import com.taocore.top.title.service.TaobaoService;
import com.taocore.top.title.service.TitleChecker;
import com.taocore.top.title.service.WhiteSpacesChecker;

@Results({
	  @Result(location="diagnose.jsp")
})
public class DiagnoseAction extends ActionBase{

	static Logger _log = Logger.getLogger(DiagnoseAction.class.getName());
	
	private long numIid;
	private String title;
	private List<DiagnoseResult> warnings;
	
	public String execute()
	{
		String session = getSessionId();
		String fields = "num_iid,title,pic_url,cid,props_name";
		try {
			ItemGetResponse rsp = TaobaoService.getItem(numIid, fields, session);
			if (rsp.isSuccess())
			{
				Item item = rsp.getItem();
				List<String> props = getPropKeywords(item.getPropsName());
				TitleChecker[] checkers = {
						new BrandsChecker(item.getCid()), new AttachmentsChecker(), new ComparationChecker(),
						new PropertyWordsChecker(props), new LengthChecker(), new WhiteSpacesChecker()
				};
				warnings = new ArrayList<DiagnoseResult>();
				for (TitleChecker checker : checkers)
				{
					_log.info("checking with: " + checker.getClass());
					DiagnoseResult result = checker.check(title);
					if (result != null)
					{
						warnings.add(result);
					}
					_log.info("checked.");
				}
			}
			else
			{
				_log.warning(TaobaoService.getError(rsp));
			}
		} catch (ApiException e) {
			_log.log(Level.SEVERE, "", e);
		}
		return SUCCESS;
	}

	private List<String> getPropKeywords(String propsName)
	{
		_log.info("propsName: " + propsName);
		if (propsName != null)
		{
			String[] props = propsName.split(";");
			List<String> keywords = new ArrayList<String>();
			for (int i = 0, n = props.length; i < n; i++)
			{
				String prop = props[i];
				if (!StringUtils.isEmpty(prop))
				{
					String propName = prop.split(":")[3];
					propName = StringUtils.replace(propName, "-", "/");
					propName = StringUtils.replace(propName, "(", "/");
					propName = StringUtils.replace(propName, ")", "/");
					String[] tokens = propName.split("/");
					keywords.addAll(Arrays.asList(tokens));
				}
			}
			return keywords;
		}
		return null;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitle() {
		return title;
	}

	public void setNumIid(long numIid) {
		this.numIid = numIid;
	}

	public long getNumIid() {
		return numIid;
	}

	public void setWarnings(List<DiagnoseResult> warnings) {
		this.warnings = warnings;
	}

	public List<DiagnoseResult> getWarnings() {
		return warnings;
	}
}
